<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');

class GalleryAlbumBase extends BaseItemBase {
	
	//constructor
	function GalleryAlbumBase() {
		parent::BaseItemBase();
		
		$this->item_link = true;
		$this->table = 'pp_galleries_albums';
		$this->prefix = 'albums_';
		$this->filter_fields = array('cid:=');
	}
	
	//methods
	function onAddImages($data_array, $target_id) {
		if( !is_array($data_array) ) {
			return false;	
		}
		$items_ids = array();
		if( is_array($data_array['albums_images']) ) {
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/gallery/gallery.image.class.php');
			$mObj = new GalleryImageBase();
			
			$cc = 0;
			foreach($data_array['albums_images'] as $k => $v) {
				if( strlen($v) > 0 ) {
					$d = array();
					$d['field_aid'] = $target_id;
					$d['field_value'] = $v;
					$d['field_width'] = $data_array['albums_images_width'][$k];
					$d['field_height'] = $data_array['albums_images_height'][$k];
					$d['field_name'] = $data_array['albums_images_filename'][$k];
					$d['field_filename'] = $data_array['albums_images_filename'][$k];
					
					$items_ids[] = $mObj->addItem($d);
					$cc++;
				}
			}
			
			//
			//$query = "update " . $this->table . " set " . $this->prefix . "images = " . $this->prefix . "images + " . $cc . " " 
			//		. " where " . $this->prefix . "id = " . (int)$target_id . "";
			//$this->adb->update($query);
			$count = $mObj->getItemsTotal(array('aid:=' => $target_id), -1);
			$this->updateItem($target_id, array('_images' => $count));
		}
		
		return $items_ids;
	}
	
	//
	function onDeleteItemAfter($items_id) {
		$query = "delete from pp_galleries_images where images_aid = '" . (int)$items_id . "'";
		
		return $this->adb->delete($query);
	}
}
//
?>